package com.lry.jvm.instructions.refrences;

import com.lry.jvm.instructions.base.NoOperandsInstruction;
import com.lry.jvm.rtda.Frame;
import com.lry.jvm.rtda.JObject;
import com.lry.jvm.rtda.OperandStack;
import com.lry.jvm.util.Util;

//arraylength指令只需要一个操作数，即从操作数栈顶弹出的数组引用
public class arraylength extends NoOperandsInstruction {

    @Override
    public byte getOpCode() {
        return (byte) 0xbe;
    }

    @Override
    public void execute(Frame frame) throws Exception {
        OperandStack stack = frame.getOperandStack();
        JObject arrRef = stack.popRef();
        if(null==arrRef){
            Util.panic("java.lang.NullPointerException");
        }

        int arrLen = arrRef.arrayLength();
        stack.pushInt(arrLen);
    }
}
